<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      :draggable="draggable"
      destroy-on-close
      :class="['cjui-dialog', useScrollbar && 'use-scroll-bar']"
      :style="{
        '--cjui-dialog-height': height,
        '--cjui-dialog-margin-top': marginTop,
      }"
    >
      <template v-if="$slots.header" #header>
        <slot name="header"></slot>
      </template>

      <el-scrollbar v-if="useScrollbar">
        <div class="cjui-dialog__body">
          <slot></slot>
        </div>
      </el-scrollbar>
      <slot v-else></slot>

      <template v-if="$slots.footer" #footer>
        <slot name="footer"></slot>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
defineOptions({
  name: 'CjuiDialog',
})

defineProps({
  draggable: {
    type: Boolean,
    default: true,
  },
  // 扩展属性
  height: {
    type: String,
    default: 'auto',
  },
  marginTop: {
    type: String,
    default: '15vh',
  },
  useScrollbar: {
    type: Boolean,
    default: true,
  },
})
</script>

<style lang="scss">
.cjui-dialog.el-dialog {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 0;
  margin: 0 auto;

  .el-dialog__body {
    position: relative;
    padding: var(--el-dialog-padding-primary);
    overflow: auto;
    flex: 1;
  }

  &:not(.is-fullscreen) {
    max-width: 90%;
    max-height: calc(100vh - 2 * var(--cjui-dialog-margin-top));
    height: var(--cjui-dialog-height);
    margin-top: var(--cjui-dialog-margin-top);

    .el-dialog__body > .el-scrollbar,
    .el-dialog__body > .el-scrollbar > .el-scrollbar__wrap {
      max-height: calc(100vh - 2 * var(--cjui-dialog-margin-top) - 122px);
    }
  }

  &.use-scroll-bar {
    .el-dialog__body {
      padding: 0;
    }
  }
}

.cjui-dialog__body {
  padding: var(--el-dialog-padding-primary);
  box-sizing: border-box;
}
</style>
